$(function(){
	var basePath=$("#basepath").val();
	$("#teacherFilter_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#teacherFilter_form select[name='department']");
		$depSelect.html("");
		var $goBtn=$("#teacherFilter_go");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>无数据</option>").appendTo($depSelect);
							$goBtn.attr("disabled",true); 
						}else{
							$goBtn.attr("disabled",false); 
						}
					}
			}, "json");
	});
	$("#teacherFilter_go").click(function(){
		var depId=$("#teacherFilter_form select[name='department']").val();
		var clgId=$("#teacherFilter_form select[name='college']").val();
		if(depId==0){
			return;
		}
		window.location=basePath+"admin/teacher/showTeacherList/"+clgId+"/"+depId+"/1";
	});
	
	$(".teacher_info").each(function(){
		var text=$(this).html();
		var maxlength=40;
		if(text.length>maxlength){
			var briefText=text.substring(0,maxlength)+"……";
			$(this).html(briefText);
			$("<br/><a href='#' class='teacher_info_more'>&lt;&lt;展开全部&gt;&gt;</a>").appendTo($(this));
			var $fullDiv= $("<div class='full_teacher_info'></div>").html(text).appendTo($(this).parent()).hide();
			$("<br/><a href='#' class='teacher_info_less'>&gt;&gt;收起全部&lt;&lt;</a>").appendTo($fullDiv);
		}
		$(this).show();
	});
	$(".teacher_info_more").click(function(e){
		e.preventDefault();
		var $lessDiv=$(this).parent();
		var $fullDiv=$lessDiv.parent().find(".full_teacher_info");
		$lessDiv.hide();
		$fullDiv.show();
	});	
	$(".teacher_info_less").click(function(e){
		e.preventDefault();
		var $fullDiv=$(this).parent();
		var $lessDiv=$fullDiv.parent().find(".teacher_info");
		$fullDiv.hide();
		$lessDiv.show();
	});
	$(".teacher_edit").click(function(){
		var teaId=$(this).attr("data-teacherId");
		window.location=basePath+"admin/teacher/showUpdateTeacher/"+teaId;
	});
	$("#teacher_add").click(function(){
		window.location=basePath+"admin/teacher/showInsertTeacher";
	});
	$(".teacher_delete").click(function(){
		$("#teacher_delete_box input[name='teaId']").val($(this).attr("data-teacherId"));
	});
	$("#teacher_delete_box_delete").click(function(){
		var teaId=$("#teacher_delete_box input[name='teaId']").val();
		$.get(basePath+ "admin/teacher/deleteTeacher/"+teaId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							noty({text:"删除错误:此教师发布了毕业设计题目或负责某毕业设计的评阅,请清除之后再试",type:"error"});
							$("#teacher_delete_box").modal('hide');
							break;
						case "-3":
							noty({text:"删除错误:此教师有教学进度数据,请清除之后再试,",type:"error"});
							$("#teacher_delete_box").modal('hide');
							break;
						case "0":
							noty({text:"删除成功,正在刷新"});
							$("#teacher_delete_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	$("#teacher_add_excel").click(function(){
		$("#teacher_add_excel_box_confirm").unbind();
		$("#teacher_add_excel_box_file_name").html("未选择");
		$("#teacher_add_excel_box_alert_container").html("");
		$("#teacher_add_excel_box_file").show();
		$("#teacher_add_excel_box_progress").hide();
		$("#teacher_add_excel_box_progress div").css("width","0%");
	});
	$("#teacher_add_excel_box_file input[type='file']").fileupload({  
	    url:basePath+"admin/teacher/insertTeacherByExcel",
	    autoUpload:false,
	    done:function(e,result){  
			noty({text:"导入完成,正在处理"});
	    	window.location=basePath+"/admin/teacher/showInsertReport";
	    } ,
	    progress:function(e,result){
	    	var progress=parseInt(result.loaded / result.total * 100, 10);
	    	$("#teacher_add_excel_box_progress div").css("width",progress+"%");
	    },
	    add:function(e,result){
	    	var fileName=result.files[0].name.toLowerCase();
			var isExcel=/(\.xlsx{0,1})$/.test(fileName);
			if(isExcel){
				$("#teacher_add_excel_box_alert_container .wrong_file_error").alert('close');
				$("#teacher_add_excel_box_file_name").html(fileName);
			}else{
				makeAlert("请选择正确的excel文件","error",$("#teacher_add_excel_box_alert_container"),"wrong_file_error");
				$("#teacher_add_excel_box_alert_container").html("请选择");
				return;
			}
			$("#teacher_add_excel_box_confirm").unbind();
			$("#teacher_add_excel_box_confirm").click(function(){
				$("#teacher_add_excel_box_file_name").html("正在上传:"+fileName);
				$("#teacher_add_excel_box_file").hide();
				$("#teacher_add_excel_box_progress").show();
				result.submit();
			});
	    }
	}) ;
});